import Vue from 'vue';

//路由引入及使用
import VueRouter from 'vue-router';
Vue.use(VueRouter);

//mint-ui
// import { Header,Swipe, SwipeItem,Button,Lazyload } from 'mint-ui';
// Vue.component(Header.name, Header);
// Vue.component(Button.name, Button);
// Vue.use(Lazyload);
// //mint-ui 轮播图
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
import MintUi from 'mint-ui';
Vue.use(MintUi);

//mui
import './lib/mui/dist/css/mui.min.css';
import './lib/mui/dist/css/icons-extra.css';

//router
import router from './router.js';
//resouse
import VueResource from 'vue-resource';
Vue.use(VueResource);
Vue.http.options.root = 'http://www.liulongbin.top:3005/';
Vue.http.options.emulateJSON = true;


// 缩略图插件
import VuePreview from 'vue-preview'
Vue.use(VuePreview)


//moment---时间格式化插件
import moment from 'moment';
Vue.filter('dateFormat',function(datestr, rule = 'YYYY-MM-DD  HH:MM') {
    return moment(datestr).format(rule);
});

//使用vuex插件 
//把car数据存在localStorage中
//验证旧car数据是否有重复的
// 有就吧count+
// 没有就新建
import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store({
    state: {
        car: JSON.parse(localStorage.getItem('car') || '[]'),
    },
    mutations: {
        addToCar(state,data) {
            var flag = false;
            state.car.some(item => {
                if(item.id == data.id ) {
                    item.count += parseInt(data.count);
                    flag = true;
                    return true;
                }
            });
            if(!flag) {
                state.car.push(data);
            }
            localStorage.setItem('car',JSON.stringify(state.car));
        },
        carChange(state,data) {
            state.car.some( item => {
                if(item.id == data.id ) {
                    item.count = parseInt(data.count);
                    return true;
                }
            });
            localStorage.setItem('car',JSON.stringify(state.car));
        },
        carRemove(state,id) {
            state.car.some((item,index) => {
                if(item.id == id ) {
                    state.car.splice(index,1);
                    return true;
                }
            });
            localStorage.setItem('car',JSON.stringify(state.car));
        },
        carButtonChang(state, data) {
            state.car.some(item => {
                if(item.id == data.id ) {
                    item.selected = data.val;
                    return true;
                }
            });
            localStorage.setItem('car',JSON.stringify(state.car));
        }
    },
    getters: {
        // TODO
        // 改变徽表数字显示
        count(state) {
            var c = 0;
            state.car.forEach(item=> {
                c += parseInt(item.count);
            });
            return c;
        },
        getCountList(state) {
            var countList = {};
            state.car.forEach(item => {
                countList[item.id] = item.count;
            });
            return countList;
        },
        getCarButton(state) {
            //先做一个list数据，包含id和true和false
            //把list传到组件中
            //点击后改变true和false
            var valList = {};
            state.car.forEach(item => {
                valList[item.id] = item.selected;
            });
            return valList;
        },
        getTotalCar(state) { //计算总价和总件数
            var data = {
                count : 0,
                amount: 0
            };
            state.car.forEach(item => {
                if( item.selected == true ) {
                    data.count += item.count;
                    data.amount += item.price * item.count;
                }
            });
            return data;
        }
    }
})




import app from './App.vue';

new Vue({
    el: '#app',
    data: {

    },
    methods: {

    },
    render: c => c(app),
    router: router,
    store: store
})